//
//  HistorySearch.m
//  BSS
//
//  Created by Juvi on 13-5-30.
//  Copyright (c) 2013年 juvid. All rights reserved.
//

#import "HistorySearch.h"
#import "DetectionSystemVersion.h"
#import "Common.h"
#import "MainSearchViewController.h"
#import "MFSideMenuContainerViewController.h"
#import "LeftMeunViewController.h"
#import "UIColor+expanded.h"
@interface HistorySearch ()

@end

@implementation HistorySearch

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([[DetectionSystemVersion alloc]Version]) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    [self BackButton];
    arrHistroy=[[NSMutableArray alloc]init];
    
    if ([Common GetHistorySearch]) {
        NSArray *arr=[[[Common GetHistorySearch]reverseObjectEnumerator] allObjects];
        [arrHistroy addObjectsFromArray:arr];
        arr=Nil;
//        for (int i=0; i<[Common GetHistorySearch].count; i++) {
//            [arrHistroy addObject:[[Common GetHistorySearch] objectAtIndex:i]];
//        }
//        arrHistroy=[Common GetHistorySearch];
    }
    if (arrHistroy.count==0) {
        self.table.hidden=YES;
    }
//    self.table.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.view.backgroundColor=[UIColor colorWithHexString:@"0xe7e7e7"];
    self.table.separatorColor=[UIColor whiteColor];
    if ([[DetectionSystemVersion alloc]Version]) {
        self.table.separatorInset=UIEdgeInsetsMake(0, 0, 0, 0);

    }
    self.table.backgroundColor=[UIColor colorWithHexString:@"0xe7e7e7"];
//    NSLog(@"%@",arrHistroy);
    // Do any additional setup after loading the view from its nib.
}
-(void)BackButton{
    self.navigationItem.titleView=[DetectionSystemVersion SetBarTitle:@"百事搜"];
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btnBack=[[DetectionSystemVersion alloc]BackButton];
    [btnBack addTarget:self action:@selector(BackReturn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem=backItem;
    
}
#pragma mark 导航栏按钮事件
-(void)BackReturn:(id)sender{
    //    [self.navigationController.view removeFromSuperview];
    [self HidePick:self.navigationController.view];
    [self.navigationController removeFromParentViewController];
}
#pragma mark 动画
-(void)HidePick:(UIView *)vieBounced {
    [UIView animateWithDuration:0.3
                     animations:^(void) {
                         vieBounced.alpha = 0.0;
                         //                         vieBounced.frame = CGRectMake(0, self.view.frame.size.height, 104, (self.view.frame.size.height-117-8)/3);
                     } completion:^(BOOL finished) {
                         
                         [vieBounced removeFromSuperview];
                     }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return arrHistroy.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      
    }
    UIView *vie=[[UIView alloc]init];
    if (indexPath.row%2==1) {
        vie.backgroundColor=[UIColor colorWithHexString:@"0xf3f3f3"];
    }
    else{
        vie.backgroundColor=[UIColor colorWithHexString:@"0xe7e7e7"];
    }
    cell.backgroundView=vie;
    cell.textLabel.font=[UIFont systemFontOfSize:16];
    cell.textLabel.textColor=[UIColor colorWithHexString:@"0x6f6f6f"];
    cell.textLabel.backgroundColor=[UIColor clearColor];
    cell.indentationLevel=1;
    cell.indentationWidth=15.0;
    cell.textLabel.text=[arrHistroy objectAtIndex:indexPath.row];
    return cell;
}
- (MFSideMenuContainerViewController *)menuContainerViewController {
    //    NSLog(@"第一个%@ 第二个%@",self.parentViewController,self.navigationController.parentViewController);
    return (MFSideMenuContainerViewController *)self.navigationController.parentViewController;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UINavigationController *navControl=[self.navigationController.parentViewController.childViewControllers objectAtIndex:1];
    [self.menuContainerViewController setMenuState:MFSideMenuStateClosed];

    [self BackReturn:Nil];
//    NSLog(@"这部%@",[arrHistroy objectAtIndex:indexPath.row]);
    if (navControl.childViewControllers.count==1) {
        MainSearchViewController *mainSearch=[[MainSearchViewController alloc]init];
        mainSearch.strSearch=[arrHistroy objectAtIndex:indexPath.row];
        [navControl pushViewController:mainSearch animated:YES ];
    }
    else if(navControl.childViewControllers.count==2){
        MainSearchViewController *mainSearch=(MainSearchViewController *)[navControl .childViewControllers objectAtIndex:1];
        [mainSearch GetDatas:[arrHistroy objectAtIndex:indexPath.row]];
    }
    else{
        MainSearchViewController *mainSearch=(MainSearchViewController *)[navControl .childViewControllers objectAtIndex:1];
        [mainSearch GetDatas:[arrHistroy objectAtIndex:indexPath.row]];

        [navControl popToViewController:mainSearch animated:YES];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)PressClean:(id)sender {
    [arrHistroy removeAllObjects];
    
    [Common SetHistorySearch:arrHistroy];
    [self.table reloadData];
    self.table.hidden=YES;
}
@end
